import { ControlledEditorProps, EditableExcelTableProps, TColumn } from '@/components/editable-excel-table';
import { selectEditorByType } from '../utils/editorUtils';
import { BaseEditorProps } from './BaseEditor';

interface CellEditorProps<C extends TColumn = TColumn> {
    isEditFlage: boolean;
    rowIndex: number;
    column: C;
    value: string;
    onClose: ControlledEditorProps['onClose'];
    editorRenderers: EditableExcelTableProps['editorRenderers'];
}

/**
 * 单元格编辑器整体入口
 */
export function CellEditor<C extends TColumn = TColumn>({
    isEditFlage,
    rowIndex,
    column,
    value,
    onClose,
    editorRenderers,
}: CellEditorProps<C>) {
    const editorProps: BaseEditorProps<C> = {
        value,
        column,
        rowIndex,
        dataIndex: column.dataIndex,
        onClose,
    };

    // 使用公用的编辑器选择函数
    const content = selectEditorByType({
        column,
        editorProps: {
            ...editorProps,
            onChange: (val) => {
                editorProps?.onChange?.(val);
                // 共享数据
                console.log('共享数据单元格', val);
            },
        },
        editorRenderers,
        isReadonly: !isEditFlage,
    });

    return <>{content}</>;
}
